<template>  
  <view class="container">  
    <view class="form-container" v-if="isLogin">  
      <text class="title">登录</text>  
      <input type="text" placeholder="用户名" v-model="username" />  
      <input type="password" placeholder="密码" v-model="password" />  
      <button @click="login">登录</button>  
      <text class="link" @click="switchToRegister">还没有账号？立即注册</text>  
	  <text class="link" @click="switchToChange">修改密码</text>
    </view>  
    <view class="form-container" v-else>  
      <text class="title">注册</text>  
      <input type="text" placeholder="用户名" v-model="newUsername" />  
      <input type="password" placeholder="密码" v-model="newPassword" />  
      <input type="password" placeholder="确认密码" v-model="confirmPassword" />  
      <button @click="register">注册</button>  
      <text class="link" @click="switchToLogin">已有账号？立即登录</text>  
    </view>  
  </view>  
</template>  
  
<script>  
export default {  
  data() {  
    return {  
      isLogin: true, // 控制显示登录还是注册表单  
      username: '', // 登录用户名  
      password: '', // 登录密码  
      newUsername: '', // 注册用户名  
      newPassword: '', // 注册密码  
      confirmPassword: '' // 确认注册密码  
    };  
  },  
  methods: {  
    // 切换至注册表单  
    switchToRegister() {  
      this.isLogin = false;  
    },  
    // 切换至登录表单  
    switchToLogin() {  
      this.isLogin = true;  
    },  
    // 登录操作
login() {
  if (!this.username || !this.password) {
    uni.showToast({ title: '请填写用户名和密码', icon: 'none' });
    return;
  }
  
  // 调用后端API进行登录验证
  uni.request({
    url: 'http://127.0.0.1:3007/api/login',
    method: 'POST',
    data: {
      username: this.username,
      password: this.password
    },
    success: (res) => {
		if(res.data.status===0)
		{
      // 处理登录成功逻辑
      uni.showToast({ title: '登录成功', icon: 'success' });
	  setTimeout(()=>{
		  uni.navigateTo({
		  	url:'/pages/log/select'
		  });
	  },1500);
    }else{
		uni.showToast({
			title:'登录失败',icon:'none'
		})
		}
	},
    fail: (err) => {
      // 处理登录失败逻辑
      uni.showToast({ title: '登录失败', icon: 'none' });
    }
  });
},

// 注册操作
register() {
  if (!this.newUsername || !this.newPassword ) {
    uni.showToast({ title: '请填写注册信息', icon: 'none' });
    return;
  }
  
  // 调用后端API进行用户注册
  uni.request({
    url: 'http://127.0.0.1/api/reguser',
    method: 'POST',
    data: {
      username: this.newUsername,
      password: this.newPassword
    },
    success: (res) => {
     if(res.data.status === 0) { // 修改此处
                     uni.showToast({ title: '注册成功', icon: 'success' });
                   } else {
                     uni.showToast({
                       title: res.data.message, // 修改此处
                       icon: 'none'
                     });
                   }
                 },
                 fail: (err) => {
                   uni.showToast({ title: '注册失败', icon: 'none' });

    }
  });
}  
  }  
};  
</script>  
  
<style scoped>  
.container {  
  display: flex;  
  flex-direction: column;  
  align-items: center;  
  justify-content: center;  
  height: 100vh;  
}  
.form-container {  
  width: 80%;  
}  
.title {  
  font-size: 20px;  
  margin-bottom: 20px;  
}  
input {  
  width: 100%;  
  padding: 10px;  
  margin-bottom: 10px;  
  border: 1px solid #ccc;  
}  
button {  
  padding: 10px 20px;  
  background-color: #4caf50;  
  color: white;  
  border: none;  
  margin-top: 10px;  
}  
.link {  
  margin-top: 20px;  
  color: blue;  
  text-decoration: underline;  
  cursor: pointer;  
}  
</style>